package com.example.wisdomclassroomapi.facade.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wisdomclassroomapi.application.service.CourseMaterialApplicationService;
import com.example.wisdomclassroomapi.common.dto.PageDto;
import com.example.wisdomclassroomapi.domain.entity.CourseMaterialEntity;
import com.example.wisdomclassroomapi.facade.assembler.CourseMaterialFacadeAssembler;
import com.example.wisdomclassroomapi.facade.controller.common.BaseController;
import com.example.wisdomclassroomapi.facade.dto.CourseMaterialRequestDto;
import com.example.wisdomclassroomapi.facade.dto.CourseMaterialResponseDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Package: com.example.wisdomclassroomapi.facade.controller.wxapp
 * @Description: <前端控制器>-相关接口
 * @Author: scholes
 * @CreateDate: 2022-04-18
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-18
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@RestController
@RequestMapping("/wxapp/courseMaterial")
@Slf4j
public class CourseMaterialController extends BaseController {
@Resource
private CourseMaterialApplicationService courseMaterialApplicationService;

@Resource
private CourseMaterialFacadeAssembler courseMaterialFacadeAssembler;

/**
* 分页查询
*/
@PostMapping("/page")
public PageDto<List<CourseMaterialResponseDto>> getCourseMaterialByPage(@RequestBody @Valid CourseMaterialRequestDto request) {
        IPage<CourseMaterialEntity> page = new Page<CourseMaterialEntity>();
        page.setSize(request.getPageSize());
        page.setCurrent(request.getPageNum());
       CourseMaterialEntity entity= courseMaterialFacadeAssembler.assembleCourseMaterialEntity(request);
        page =  courseMaterialApplicationService.page(entity, page);
        List<CourseMaterialResponseDto> assembleData = new ArrayList<>();
        if (page != null&& page.getRecords()!=null&&page.getRecords().size() > 0) {
        for (CourseMaterialEntity item : page.getRecords()) {
           assembleData.add(courseMaterialFacadeAssembler.assembleCourseMaterialResponseDto(item));
        }
        }
        PageDto<List<CourseMaterialResponseDto>> result = new PageDto<>();
        result.setPageSize((int) page.getSize());
        result.setPageNum((int) page.getCurrent());
        result.setData(assembleData);
        return result;
}


/**
* 查询
*/
@PostMapping("/list")
public List<CourseMaterialResponseDto> getCourseMaterials(@RequestBody @Valid CourseMaterialRequestDto request) {
       CourseMaterialEntity entity= courseMaterialFacadeAssembler.assembleCourseMaterialEntity(request);
        List<CourseMaterialEntity> list= courseMaterialApplicationService.list(entity);
        List<CourseMaterialResponseDto> assembleData = new ArrayList<>();
        if(list!=null&&list.size()>0){
           for (CourseMaterialEntity item : list) {
              assembleData.add(courseMaterialFacadeAssembler.assembleCourseMaterialResponseDto(item));
           }
        }
        return assembleData;
}
/**
* 获取某一实体
*/
@PostMapping("/getCourseMaterial")
public CourseMaterialResponseDto getCourseMaterialDetails(String id) {
       CourseMaterialEntity entity = courseMaterialApplicationService.findById(id);
       CourseMaterialResponseDto responseDto=null;
        if(entity!=null){
            responseDto= courseMaterialFacadeAssembler.assembleCourseMaterialResponseDto(entity);
        }
        return responseDto;
}
/**
* 新增数据
*/
@PostMapping("/add")
public void saveCourseMaterial(@RequestBody CourseMaterialRequestDto request) {
       CourseMaterialEntity entity= courseMaterialFacadeAssembler.assembleCourseMaterialEntity(request);
       courseMaterialApplicationService.add(entity);
}
/**
* 修改数据
*/
@PostMapping("/update")
public void modifyCourseMaterial(@RequestBody CourseMaterialRequestDto request) {
       CourseMaterialEntity entity= courseMaterialFacadeAssembler.assembleCourseMaterialEntity(request);
       courseMaterialApplicationService.updateById(entity);
}

/**
* 删除数据
*/
@PostMapping("/delete")
public void removeCourseMaterial(@RequestBody @Valid  String id) {
       courseMaterialApplicationService.deleteById(id);
}
}